package com.school.extra.base;

import com.school.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import zhibi.commons.exception.MessageException;
import zhibi.frame.context.Constant;
import zhibi.frame.springmvc.base.AbstractController;
import zhibi.utils.io.FileUtils;

@Component
public abstract class BaseController extends AbstractController {

    @Value("${upload.path}")
    private String uploadPath;


    public Integer pageSize = 10;

    /**
     * 登录的用户
     *
     * @return
     */
    protected User sessionUser() {
        return (User) session.getAttribute(Constant.SESSION.ADMIN);
    }

    protected void checkAdmin() {
        if (!sessionUser().getType().equals("admin")) throw new MessageException("普通用户没有权限");
    }


    protected String forward(String view, String tipMsg) {
        request.setAttribute("tipMsg", tipMsg);
        return view;
    }

    protected String saveFile(MultipartFile file) {
        String path = System.getProperty("user.dir", "NULL");
        path = path.replace("\\", "_").replace(":", "_");
        return FileUtils.saveFile(file, uploadPath, path);
    }

}
